Repository navigation
Say a C# local's type when it has no initialiser to infer from [patch] - #356
Merged
Merged
Conversation
C# infers `var` only from an initialiser (CS0818), so a local declared with no initial value now uses its declared type even when Inferred is ticked, and falls back to `object` with a note when it has no type either. Locals with an initialiser are still written as `var`. Fixes #355 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW34ihN37UX2iCsueam
|
This was referenced Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #355
What changed
CSharpGenerator.GenerateVariableDeclarationwrotevarwheneverIsTypeInferredwas set orTypewas null, including when there was no initialiser. C# can only infer from an initialiser, so this producedvar x;, which fails with CS0818.The generator now writes:
varonly when there is an initialiser to infer fromint typed;)objectwith an inexpressible note when there is neither a type nor an initialiser (object untyped;). This matches the existing fallback for held variables.A held (file/namespace-scope) variable that is inferred, has a declared type and has no initialiser now also gets its declared type, not
object.Tests
CSharpGeneratedSourceCompilesTests.LocalsWithoutAnInitialiser_Compilecompiles all three cases withdotnet build. I checked that it fails onmain(the output wasvar untyped; var typed;) and passes with the fix.Coder.Testsuite: 1552 passed, 0 failed.Generated by Claude Code